﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ManageBanking.Areas.Admin.Models;
namespace ManageBanking.Areas.Admin.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Admin/Search/

        private SearchModels sm = new SearchModels();
        public ActionResult SearchUser(string searchcontent, string typesearch)
        {
            if (Session["Admin"] != null && searchcontent != null && typesearch != null)
            {
                Session["searchcontent"] = searchcontent;
                Session["typesearch"] = typesearch;
                ViewData["Check"] = Session["Check"];
                Session.Remove("Check");
                int totalUsername;
                try
                {
                    if (typesearch.Equals("ID"))
                    {
                        ViewData["listuser"] = sm.SearchUserID(Convert.ToInt32(searchcontent), out totalUsername);
                        ViewData["totaluser"] = totalUsername;
                        if (totalUsername == 0)
                        {
                            Session["error"] = "Can't Find Data From" + searchcontent;
                            return RedirectToAction("MessgaseError", "Search");
                        }
                    }
                    else if (typesearch.Equals("Name"))
                    {
                        ViewData["listuser"] = sm.SearchUserName(searchcontent, out totalUsername);
                        ViewData["totaluser"] = totalUsername;
                        if (totalUsername == 0)
                        {
                            Session["error"] = "Can't Find Data From " + searchcontent;
                            return RedirectToAction("MessgaseError", "Search");
                        }
                    }
                    else if (typesearch.Equals("date"))
                    {
                        ViewData["listuser"] = sm.SearchUserDate(Convert.ToDateTime(searchcontent), out totalUsername);
                        ViewData["totaluser"] = totalUsername;
                        if (totalUsername == 0)
                        {
                            Session["error"] = "Can't Find Data From" + searchcontent;
                            return RedirectToAction("MessgaseError", "Search");
                        }
                    }
                    else
                    {
                        ViewData["listuser"] = sm.SearchUserAll(out totalUsername);
                        ViewData["totaluser"] = totalUsername;
                    }
                }
                catch
                {
                    Session["error"] = "Can't Find Data From ";
                    return RedirectToAction("MessgaseError", "Search");
                }
                return View();
            }
            else
                return RedirectToAction("LoginAdmin", "LoginAdmin");
        }


        public ActionResult SearchUser1(string searchcontent1, string typesearch)
        {
            if (Session["Admin"] != null && searchcontent1 != null && typesearch != null)
            {
                ViewData["menuadmin"] = "RequestFromUser";
                Session["searchcontent"] = searchcontent1;
                Session["typesearch"] = typesearch;
                ViewData["Check"] = Session["Check"];
                Session.Remove("Check");
                int totalUsername=0;
                try
                {
                    if (typesearch.Equals("Username"))
                    {
                        if (Session["Admin"] != null)
                        {
                            ManageAdminModels ma = new ManageAdminModels();
                            int a = ma.iduser(searchcontent1);
                            ViewData["listuser"] = sm.SearchRequestAdminUsername(a, out totalUsername);
                            ViewData["totaluser"] = totalUsername;
                            if (totalUsername == 0)
                            {
                                Session["error"] = "Can't Find Data From " + searchcontent1;
                                return RedirectToAction("MessgaseError1", "Search");
                            }

                            if (Session["Res"] != null)
                                ViewData["Res"] = Session["Res"];
                            Session.Remove("Res");
                            return View();
                        }
                        else
                            return RedirectToAction("LoginAdmin", "LoginAdmin");

                        
                    }
                    else if (typesearch.Equals("date"))
                    {
                        if (Session["Admin"] != null)
                        {
                            ViewData["listuser"] = sm.SearchRequestAdminDate(Convert.ToDateTime(searchcontent1), out totalUsername);
                            ViewData["totaluser"] = totalUsername;
                            if (totalUsername == 0)
                            {
                                Session["error"] = "Can't Find Data From " + searchcontent1;
                                return RedirectToAction("MessgaseError1", "Search");
                            }

                            if (Session["Res"] != null)
                                ViewData["Res"] = Session["Res"];
                            Session.Remove("Res");
                            return View();
                        }
                        else
                            return RedirectToAction("LoginAdmin", "LoginAdmin");
                    }
                    else if (typesearch.Equals("Slove") || typesearch.Equals("UnSlove"))
                    {
                        if (Session["Admin"] != null)
                        {
                            if (typesearch.Equals("Slove"))
                            {
                                ViewData["listuser"] = sm.SearchRequestAdminSolved(true, out totalUsername);
                                ViewData["totaluser"] = totalUsername;
                                if (totalUsername == 0)
                                {
                                    Session["error"] = "Can't Find Data From " + typesearch;
                                    return RedirectToAction("MessgaseError1", "Search");
                                }
                            }
                            else if (typesearch.Equals("UnSlove"))
                            {
                                ViewData["listuser"] = sm.SearchRequestAdminSolved(false, out totalUsername);
                                ViewData["totaluser"] = totalUsername;
                                if (totalUsername == 0)
                                {
                                    Session["error"] = "Can't Find Data From " + typesearch;
                                    return RedirectToAction("MessgaseError1", "Search");
                                }
                            }

                            if (Session["Res"] != null)
                                ViewData["Res"] = Session["Res"];
                            Session.Remove("Res");
                            return View();
                        }
                        else
                            return RedirectToAction("LoginAdmin", "LoginAdmin");
                    }
                    else
                    {
                        ViewData["listuser"] = sm.SearchRequestAdminAll(out totalUsername);
                        ViewData["totaluser"] = totalUsername;
                        if (totalUsername == 0)
                        {
                            Session["error"] = "Can't Find Data From " + searchcontent1;
                            return RedirectToAction("MessgaseError1", "Search");
                        }
                    }
                }
                catch
                {
                    Session["error"] = "Can't Find Data From date";
                    return RedirectToAction("MessgaseError1", "Search");
                }
                return View();
            }
            else
                return RedirectToAction("LoginAdmin", "LoginAdmin");
        }



        public  ActionResult MessgaseError()
        {
            return View();
        }
        public ActionResult MessgaseError1()
        {
            return View();
        }


        public ActionResult UpdateSearch(string idusername, string lockuser)
        {
            if (Session["Admin"] != null && idusername != null && lockuser != null)
            {
                string searchcontent = Session["searchcontent"].ToString();
                string typesearch = Session["typesearch"].ToString(); ;
                if (sm.Update(Convert.ToInt32(idusername), lockuser) == true)
                    Session["Check"] = "true";
                else
                    Session["Check"] = "Update Fail, Please Try Again";

                return RedirectToAction("SearchUser/" + searchcontent + "/" + typesearch, "Search");
            }
            else
                return RedirectToAction("LoginAdmin", "LoginAdmin");
        }

    }
}
